package com.xxdemo.quickstart;

import com.xxdemo.quickstart.database.table.Column;

public class GenericQuerySettings implements QuerySettings {
    public static final Integer MAX_RECORDS_ALLOWED = 500;
    public static final String DEFAULT_SORT_BY_COLUMN_NAME = "`id`";
    private static final Column _DefaultColumn = new Column(DEFAULT_SORT_BY_COLUMN_NAME);
    private String sortBy;
    private SortOrderKeyword sortOrder = SortOrderKeyword.ASCEND;
    private Integer limit;

    public GenericQuerySettings() {
        this(_DefaultColumn, SortOrderKeyword.ASCEND, MAX_RECORDS_ALLOWED);
    }

    public GenericQuerySettings(Integer limit) {
        this(_DefaultColumn, SortOrderKeyword.ASCEND, limit);
    }

    public GenericQuerySettings(Column sortBy, SortOrderKeyword sortOrder, Integer limit) {
        setSortBy(sortBy);
        setOrderIsDesc(SortOrderKeyword.DESCEND.equals(sortOrder));
        setLimit(limit);
    }

    @Override
    public String getSortBy() {
        return sortBy;
    }

    @Override
    public boolean getOrderIsDesc() {
        return SortOrderKeyword.DESCEND.equals(sortOrder);
    }

    @Override
    public Integer getLimit() {
        return limit;
    }

    public void setSortBy(Column column) {
        sortBy = column.getName();
    }

    public void setOrderIsDesc(boolean orderIsDesc) {
        sortOrder = orderIsDesc ?
                SortOrderKeyword.DESCEND : SortOrderKeyword.ASCEND;
    }

    public void setLimit(Integer limit) {
        if (limit <= 0 || limit > MAX_RECORDS_ALLOWED) {
            limit = MAX_RECORDS_ALLOWED;
        }
        this.limit = limit;
    }
}
